#include"Queue.h"
void QueueInit(Queue* pq)
{
	assert(pq);
	pq->head = pq->tail = NULL;
	pq->size = 0;
}

//push
void QueuePush(Queue* pq, QueueDatatype x)
{
	assert(pq);
	QueueNode* newnode = (QueueNode*)malloc(sizeof(QueueNode));
	if (!newnode)
	{
		perror("malloc fail");
	}
	newnode->next = NULL;
	newnode->val = x;
	if (!pq->head && !pq->tail)
	{
		pq->head = pq->tail = newnode;
	}
	else
	{
		pq->tail->next = newnode;
		pq->tail = newnode;

	}
	pq->size++;
}



//pop
void QueuePop(Queue* pq)
{
	assert(pq);
	
	assert(!is_empty(pq));
	QueueNode* del = pq->head;
	pq->head = del->next;

	if (pq->size == 1)
	{
		pq->tail = NULL;
	}
	free(del);
	del = NULL;
	pq->size--;
}
QueueDatatype QueueTop(Queue* pq)
{
	return pq->head->val;
}


//is empty
bool is_empty(Queue* pq)
{
	assert(pq);
	return pq->size == 0;
}


//destroy
void Destroy(Queue* pq)
{
	QueueNode* cur = pq->head;
	while (cur)
	{
		QueueNode* next = cur->next;
		free(cur);
		cur = next;
	}
	pq->head = pq->tail = NULL;
	pq->size = 0;
}